<?php

namespace App\Models;

class Footprints extends Base
{
    protected $table = 'footprints';

    protected $fillable = [
        'id','user_id','foot_id','type','created_at','updated_at'
    ];

    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function allLists($params = [], $fields = ['*'], $limit = 10, $page = null){
        if(isset($page)){
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['id']) && $params['id']) {
                        $query ->whereIn('id',$params['id']);
                    }
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                })->orderBy('created_at', 'desc')->paginate($limit, null, null, $page);
        }else{
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['id']) && $params['id']) {
                        $query ->whereIn('id',$params['id']);
                    }
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                })->orderBy('created_at', 'desc')->limit($limit)->get();
        }
        foreach ($lists as $key => $val) {
            if($val['type'] == 1){
                $field = ['id','store_name','logo','call','view','service','praise','service_scope','hot','quality'];
                $lists[$key]['store'] = Stores::where(['id'=>$val['foot_id']])->first($field);
                $lists[$key]['store']['view'] = strlen($lists[$key]['store']['view'])>=4 ? '999+' : $lists[$key]['store']['view'];
                $lists[$key]['store']['call'] = strlen($lists[$key]['store']['call'])>=4 ? '999+' : $lists[$key]['store']['call'];
                $lists[$key]['store']['service'] = strlen($lists[$key]['store']['service'])>=4 ? '999+' : $lists[$key]['store']['service'];
                $lists[$key]['store']['praise'] = strlen($lists[$key]['store']['praise'])>=4 ? '999+' : $lists[$key]['store']['praise'];
            }elseif ($val['type'] == 2) {
                $field = ['id','price','title','content','number','created_at','end_at','urgent'];
                $lists[$key]['demand'] = Demands::where(['id'=>$val['foot_id']])->first($field);
                $lists[$key]['demand']['number'] = strlen($lists[$key]['demand']['number'])>=4 ? '999+' : $lists[$key]['demand']['number'];
                $lists[$key]['demand']['created_at'] = trim(@date('Y-m-d', strtotime($lists[$key]['demand']['created_at'])));
                $lists[$key]['demand']['end_at'] = trim(@date('Y-m-d', strtotime($lists[$key]['demand']['end_at'])));
                $lists[$key]['demand']['price'] = floatval($lists[$key]['demand']['price']);
            }elseif ($val['type'] == 3) {
                $field = ['id','title','thumb','price','description','salenum','type','view'];
                $lists[$key]['goods'] = Goods::where(['id'=>$val['foot_id']])->first($field);
                $lists[$key]['goods']['view'] = strlen($lists[$key]['goods']['view'])>=4 ? '999+' : $lists[$key]['goods']['view'];
                $lists[$key]['goods']['salenum'] = strlen($lists[$key]['goods']['salenum'])>=4 ? '999+' : $lists[$key]['goods']['salenum'];
            }elseif ($val['type'] == 4) {
                $field = ['id','title','thumb','view','number','cost','start_at'];
                $lists[$key]['act'] = Activitys::where(['id'=>$val['foot_id']])->first($field);
                $lists[$key]['act']['view'] = strlen($lists[$key]['act']['view'])>=4 ? '999+' : $lists[$key]['act']['view'];
                $lists[$key]['act']['number'] = strlen($lists[$key]['act']['number'])>=4 ? '999+' : $lists[$key]['act']['number'];
                $lists[$key]['act']['start_at'] = trim(@date('Y-m-d', strtotime($lists[$key]['act']['start_at'])));
            }elseif ($val['type'] == 5) {
                $field = ['id','title','thumb','view','download','format','created_at'];
                $lists[$key]['word'] = Words::where(['id'=>$val['foot_id']])->first($field);
                $lists[$key]['word']['view'] = strlen($lists[$key]['word']['view'])>=4 ? '999+' : $lists[$key]['word']['view'];
                $lists[$key]['word']['download'] = strlen($lists[$key]['word']['download'])>=4 ? '999+' : $lists[$key]['word']['download'];
            }
        }
        return $lists;
    }

    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function apiLists($params = [], $fields = ['*']){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['id']) && $params['id']) {
                    $query ->whereIn('id',$params['id']);
                }
            })->orderBy('created_at', 'desc')->limit(10)->get();
        foreach ($lists as $key => $val) {
            if($val['type'] == 1){
                $field = ['id','store_name','logo','call','view','service','praise','service_scope','hot','quality'];
                $store = Stores::where(['id'=>$val['foot_id']])->first($field);
                if(!empty($store)){
                    $store['view'] = strlen($store['view'])>=4 ? '999+' : $store['view'];
                    $store['call'] = strlen($store['call'])>=4 ? '999+' : $store['call'];
                    $store['service'] = strlen($store['service'])>=4 ? '999+' : $store['service'];
                    $store['praise'] = strlen($store['praise'])>=4 ? '999+' : $store['praise'];
                }
                $lists[$key]['store'] = $store;
            }elseif ($val['type'] == 2) {
                $field = ['id','price','title','content','number','created_at','end_at','urgent'];
                $demand = Demands::where(['id'=>$val['foot_id']])->first($field);
                if(!empty($demand)){
                    $demand['start_at'] = trim(@date('Y-m-d', strtotime($demand['created_at'])));
                    $demand['end_at'] = trim(@date('Y-m-d', strtotime($demand['end_at'])));
                    $demand['price'] = floatval($demand['price']);
                }
                $lists[$key]['demand'] = $demand;
            }elseif ($val['type'] == 3) {
                $field = ['id','title','thumb','price','description','salenum','type','view'];
                $goods = Goods::where(['id'=>$val['foot_id']])->first($field);
                if(!empty($goods)){
                    $goods['view'] = strlen($goods['view'])>=4 ? '999+' : $goods['view'];
                    $goods['salenum'] = strlen($goods['salenum'])>=4 ? '999+' : $goods['salenum'];
                }
                $lists[$key]['goods'] = $goods;
            }elseif ($val['type'] == 4) {
                $field = ['id','title','thumb','view','number','cost','start_at'];
                $act = Activitys::where(['id'=>$val['foot_id']])->first($field);
                if(!empty($act)){
                    $act['view'] = strlen($act['view'])>=4 ? '999+' : $act['view'];
                    $act['number'] = strlen($act['number'])>=4 ? '999+' : $act['number'];
                    $act['start_at'] = trim(@date('Y-m-d', strtotime($act['start_at'])));
                    $now_time = date('Y-m-d H:i:s');
                    if($now_time <= $act['enroll_at']){
                        $act['act_state'] = '报名中';
                    }elseif ($now_time >= $act['start_at']){
                        $act['act_state'] = '已举办';
                    }else {
                        $act['act_state'] = '报名结束';
                    }
                }
                $lists[$key]['act'] = $act;
            }elseif ($val['type'] == 5) {
                $field = ['id','title','thumb','view','download','format','created_at'];
                $word = Words::where(['id'=>$val['foot_id']])->first($field);
                if(!empty($word)){
                    $word['view'] = strlen($word['view'])>=4 ? '999+' : $word['view'];
                    $word['download'] = strlen($word['download'])>=4 ? '999+' : $word['download'];
                }
                $lists[$key]['word'] = $word;
            }
        }
        return $lists;
    }


}
